/*******************************************************************************
 * Package: com.hngtrust.iir.user.controller
 * Type:    CollectController
 * Date:    2021/3/16 16:40
 *
 * Copyright (c) 2021 HUANENG GUICHENG TRUST CORP.,LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.hngtrust.iir.user.controller;

import com.hngtrust.framework.bind.ResponseInfo;
import com.hngtrust.framework.id.service.MyIdGenerator;
import com.hngtrust.framework.page.PageResult;
import com.hngtrust.framework.page.service.PageService;
import com.hngtrust.framework.security.util.SecurityUtils;
import com.hngtrust.iir.base.controller.vo.OptLogReqVO;
import com.hngtrust.iir.base.service.LogService;
import com.hngtrust.iir.company.entity.Company;
import com.hngtrust.iir.company.service.CompanyService;
import com.hngtrust.iir.constant.Constant;
import com.hngtrust.iir.dto.ReqDTO;
import com.hngtrust.iir.risk.entity.RiskMonitor;
import com.hngtrust.iir.risk.service.RiskMonitorService;
import com.hngtrust.iir.user.controller.dto.DeleteCollectVO;
import com.hngtrust.iir.user.controller.dto.DeleteGroupVO;
import com.hngtrust.iir.user.controller.dto.ListCollectVO;
import com.hngtrust.iir.user.controller.dto.ViewCollectVO;
import com.hngtrust.iir.user.controller.dto.ViewGroupVO;
import com.hngtrust.iir.user.dto.CollectGroupVO;
import com.hngtrust.iir.user.dto.ListCollectRespVO;
import com.hngtrust.iir.user.entity.Collect;
import com.hngtrust.iir.user.entity.CollectGroup;
import com.hngtrust.iir.user.entity.User;
import com.hngtrust.iir.user.service.CollectGroupService;
import com.hngtrust.iir.user.service.CollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * 收藏管理
 *
 * @author Keqiwei
 * @date 2021/3/16 16:40
 */
@Slf4j
@Api(tags = "收藏管理")
@RestController
@RequestMapping("/collect")
public class CollectController {
    @Autowired
    private CollectService collectService;
    
    @Autowired
    private CollectGroupService collectGroupService;
    
    @Autowired
    private CompanyService companyService;
    
    @Autowired
    private RiskMonitorService riskMonitorService;
    
    @Autowired
    private PageService pageService;
    
    @Autowired
    private MyIdGenerator myIdGenerator;
    
    @Autowired
    private LogService logService;
    
    @ApiOperation("收藏列表")
    @PostMapping("/list-collect")
    public ResponseInfo<PageResult<ListCollectRespVO>> listCollect(
            @ApiParam(value = "收藏列表") @Valid @RequestBody ReqDTO<ListCollectVO> dto){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        
        PageResult<ListCollectRespVO> page = pageService
                .page(dto.getPageParam(), () -> collectService.queryListCollect(loginUser.getId(),dto.getContent().getGroupId()));
    
        for (ListCollectRespVO row : page.getRows()) {
            row.setGroupName(collectGroupService.queryById(row.getGroupingId()).getName());
            RiskMonitor riskMonitor = new RiskMonitor();
            riskMonitor.setCompId(row.getCompId());
            riskMonitor.setCreateUserId(loginUser.getId());
            List<RiskMonitor> riskMonitorList = riskMonitorService.queryList(riskMonitor);
            if(riskMonitorList != null && !riskMonitorList.isEmpty()){
                row.setStatus(riskMonitorList.get(0).getStatus());
            }else{
                row.setStatus(1);
            }
        }
        
        return new ResponseInfo<>(page);
    }
    
    
    @ApiOperation("添加收藏")
    @PostMapping("/add-collect")
    public ResponseInfo<Object> addCollect(@ApiParam(value = "添加参数") @Valid @RequestBody ViewCollectVO collectVO){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        Collect collect = new Collect();
        collect.setId(myIdGenerator.nextId());
        collect.setCompId(collectVO.getCompId());
        collect.setCompName(collectVO.getCompName());
        collect.setGroupingId(collectVO.getGroupingId());
        collect.setCreateTime(new Date());
        collect.setCreateUserId(loginUser.getId());
        collectService.add(collect);
        //添加日志
        OptLogReqVO optLog = new OptLogReqVO();
        optLog.setOptModule(Constant.LOG_OPTMODULE_FAVORITE);
        optLog.setOptType(Constant.LOG_OPTTYPE_ADD);
        optLog.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
        optLog.setOptContent("收藏"+collectVO.getCompName());
        logService.saveOptLog(optLog);
        
        return new ResponseInfo<Object>();
    }
    
    
    @ApiOperation("删除收藏")
    @PostMapping("/delete-collect")
    public ResponseInfo<Object> deleteCollect(@ApiParam(value = "删除参数") @Valid @RequestBody DeleteCollectVO collectVO){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        Collect collect = new Collect();
        collect.setCreateUserId(loginUser.getId());
        collect.setCompId(collectVO.getCompId());
        collectService.delete(collect);
        //添加日志
        OptLogReqVO optLog = new OptLogReqVO();
        optLog.setOptModule(Constant.LOG_OPTMODULE_FAVORITE);
        optLog.setOptType(Constant.LOG_OPTTYPE_DEELTE);
        optLog.setStatus(Constant.LOG_OPTSTATE_SUCCESS);
        Company company = companyService.queryById(collectVO.getCompId());
        if(null != company){
            optLog.setOptContent("删除"+company.getName());
        }
        logService.saveOptLog(optLog);
        
        return new ResponseInfo<Object>();
    }
    
    @ApiOperation("移动分组")
    @PostMapping("/move-group")
    public ResponseInfo<Object> moveGroup(@ApiParam(value = "收藏参数") @Valid @RequestBody ViewCollectVO collectVO){
        Collect collect = new Collect();
        BeanUtils.copyProperties(collectVO,collect);
        collectService.modifyById(collect);
        return new ResponseInfo<Object>();
    }
    
    @ApiOperation("分组列表")
    @PostMapping("/list-collect_group")
    public ResponseInfo<List<CollectGroupVO>> listCollectGroup(){
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        List<CollectGroupVO> collectGroupVOS = collectService.countGroup(loginUser.getId());
        return new ResponseInfo<List<CollectGroupVO>>(collectGroupVOS);
    }
    
    @ApiOperation("添加分组")
    @PostMapping("/add-group")
    public ResponseInfo<Object> addGroup(@ApiParam(value = "分组参数") @Valid @RequestBody ViewGroupVO group){
        CollectGroup cg = new CollectGroup();
        cg.setName(group.getName());
        User loginUser = (User) SecurityUtils.getLoginUser().getAdditionalInfo();
        cg.setCreateUserId(loginUser.getId());
        ResponseInfo<Object> responseInfo = new ResponseInfo<>();
        OptLogReqVO vo1 = new OptLogReqVO();
        vo1.setOptContent("添加"+group.getName()+"分组");
        vo1.setOptType(6);
        vo1.setOptModule(11);
        if(collectGroupService.queryList(cg).isEmpty()) {
            Long id = myIdGenerator.nextId();
            CollectGroup collectGroup = new CollectGroup();
            BeanUtils.copyProperties(group, collectGroup);
            collectGroup.setId(id);
            collectGroup.setCreateTime(new Date());
            collectGroup.setCreateUserId(loginUser.getId());
            collectGroupService.add(collectGroup);
            ViewGroupVO vo = new ViewGroupVO();
            vo.setId(id);
            vo.setName(group.getName());
            responseInfo.setData(vo);
            vo1.setStatus(1);
        }else {
            responseInfo.setMessage("分组名已存在");
            responseInfo.setCode("1");
            vo1.setStatus(2);
        }
        logService.saveOptLog(vo1);
        return responseInfo;
    }
    
    @ApiOperation("删除分组")
    @PostMapping("/delete-group")
    public ResponseInfo<Object> deleteGroup(@ApiParam(value = "分组参数") @Valid @RequestBody DeleteGroupVO group){
        collectService.deleteByGroupId(group.getId());
        collectGroupService.deleteById(group.getId());
        return new ResponseInfo<>();
    }
    
    @ApiOperation("编辑分组")
    @PostMapping("/update-group")
    public ResponseInfo<Object> updateGroup(@ApiParam(value = "分组参数") @Valid @RequestBody ViewGroupVO group){
        CollectGroup collectGroup = new CollectGroup();
        BeanUtils.copyProperties(group,collectGroup);
        collectGroupService.modifyById(collectGroup);
        return new ResponseInfo<>();
    }
    
}
